package com.tfy.liquor.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tfy.liquor.bean.CategoryInfo;
import com.tfy.liquor.bean.MyPage;
import com.tfy.liquor.bean.Result;
import com.tfy.liquor.dao.CategoryInfoDao;
import com.tfy.liquor.service.CategoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CategoryInfoServiceImpl implements CategoryInfoService {
    @Autowired
    private CategoryInfoDao categoryInfoDao;

    // 添加类别新
    @Override
    public Boolean addCategoryInfo(CategoryInfo categoryInfo) {
        return categoryInfoDao.addCategoryInfo(categoryInfo) > 0;
    }

    // 查询类别信息
    @Override
    public MyPage getCategoryInfo(Integer pageNum, Integer pageSize) {
        // 如果传入的参数是负数，则默认为1
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<CategoryInfo> list = categoryInfoDao.getCategoryInfo();
        // 可以将结果转换为 Page ,
        return new MyPage((Page) list);
    }

    // 获取所有类别信息
    @Override
    public Result getAllCategoryInfo() {
        ArrayList<CategoryInfo> list = categoryInfoDao.getCategoryInfo();
        // 可以将结果转换为 Page ,
        return new Result(list).ok();
    }

    @Override
    public MyPage getAllCategoryInfo(Integer pageNum, Integer pageSize, String categoryName) {
        // 如果传入的参数是负数，则默认为1
        pageNum = Math.max(pageNum, 1);
        pageSize = pageSize < 1 ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<CategoryInfo> list = categoryInfoDao.getAllCategoryInfo(categoryName);
        // 可以将结果转换为 Page ,
        return new MyPage((Page) list);
    }

    // 修改类别信息
    @Override
    public Boolean editCategoryInfo(CategoryInfo categoryInfo) {
        return categoryInfoDao.editCategoryInfo(categoryInfo) > 0;
    }

    // 停用类别信息
    @Override
    public Boolean stopCategoryInfo(CategoryInfo categoryInfo) {
        return categoryInfoDao.stopCategoryInfo(categoryInfo) > 0;
    }

   // 启用类别
    @Override
    public Boolean enableCategoryInfo(CategoryInfo categoryInfo) {
        return categoryInfoDao.enableCategoryInfo(categoryInfo) > 0;
    }
}
